package com.metadata_system.system.response;

import com.alibaba.fastjson.JSON;
import com.metadata_system.common.db.entity.ProjectNode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class ProjectUserResponse {
    private Integer id;
    private Integer projectId;
    private Object role;
    private Integer userId;
    private Integer userIcon;
    private Object userName;
    private Object userUsername;
    private List<Map> metadataNodeProperties;
    private Integer total;

    public static ProjectUserResponse fromEntity(ProjectNode projectNode) {
        return ProjectUserResponse.builder()
                .id(projectNode.getId())
                .projectId(projectNode.getProjectId())
                .userId(projectNode.getMetadataNodeId())
                .metadataNodeProperties(JSON.parseArray(projectNode.getMetadataNodeProperties(), Map.class))
                .total(projectNode.getDataTotal())
                .build();
    }

    public static List<ProjectUserResponse> fromEntityList(List<ProjectNode> projectNodeList) {
        return projectNodeList.stream().map(ProjectUserResponse::fromEntity).collect(Collectors.toList());
    }
}
